<template>
  <el-dialog :title="'查找会员'" :close-on-click-modal="false" :visible.sync="visible" @closed="cancel" width="80%">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-row>
        <el-form-item label="搜索会员卡号">
          <el-input v-model="dataForm.cardNo" placeholder clearable></el-input>
        </el-form-item>
        <el-form-item label="搜索会员">
          <el-input v-model="dataForm.memberInfo" placeholder="会员姓名/电话" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      highlight-current-row
      v-loading="dataListLoading"
      @current-change="handleCurrentChange"
      style="width: 100%;"
      height="300px"
    >
      <el-table-column label="选择" width="55">
        <template slot-scope="scope">
          <el-radio v-model="currentVipCard" :label="scope.row">
            <i></i>
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="cardNo" header-align="center" align="center" label="会员卡号" width="165"></el-table-column>
      <el-table-column prop="companyName" header-align="center" align="center" label="所属公司"></el-table-column>
      <el-table-column prop="memberName" header-align="center" align="center" label="用户姓名"></el-table-column>
      <el-table-column prop="memberMobile" header-align="center" align="center" label="用户电话"></el-table-column>
      <el-table-column prop="cardTypeStr" header-align="center" align="center" label="卡类型"></el-table-column>
      <el-table-column header-align="center" align="center" label="余额/积分/剩余次数">
        <template slot-scope="scope">
          <!-- 次卡 -->
          <span
            v-if="scope.row.cardType === 1 && scope.row.cardTimesType === 1"
          >{{scope.row.cardTimes}}次</span>
          <span v-if="scope.row.cardType === 1 && scope.row.cardTimesType === 0">不限次</span>
          <!-- 充值卡 -->
          <span v-if="scope.row.cardType === 2">{{scope.row.cardBalance}}元</span>
          <!-- 积分卡 -->
          <span v-if="scope.row.cardType === 3">{{scope.row.cardPoints}}积分</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="开卡日期">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.createTime,'YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="失效日期">
        <template slot-scope="scope">
          <!-- 次卡 -->
          <span v-if="scope.row.cardType == 1">{{ dateFormat(scope.row.expireTime,'YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel()">取消</el-button>
      <el-button
        type="primary"
        :disabled="currentVipCard == null || currentVipCard.id == null"
        @click="confirm()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    companyId: {
      type: Number,
      default: function() {
        return null;
      }
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        cardNo: "",
        memberInfo: "",
        companyId: this.companyId
      },
      // 当前选中的会员卡
      currentVipCard: {
        id: null
      },
      cardTypes: [
        { label: "次卡", value: 1 },
        { label: "充值卡", value: 2 },
        { label: "积分卡", value: 3 }
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false
    };
  },
  components: {},
  activated() {},
  methods: {
    init() {
      this.dataForm.companyId = this.companyId;
      this.pageIndex = 1;
      this.pageSize = 10;
      this.totalPage = 0;
      this.currentVipCard = {
        id: null
      };
      this.visible = true;
      this.$nextTick(() => {
        this.getDataList();
      });
    },
    // 获取数据列表
    getDataList() {
      if (this.companyId == null) {
        this.$message.error("请选择收银店铺");
        return;
      }
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/member/vipCard/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          cardNo: this.dataForm.cardNo,
          memberInfo: this.dataForm.memberInfo,
          companyId: this.dataForm.companyId,
          cash: "cash"
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 行选择
    handleCurrentChange(val) {
      this.currentVipCard = val;
    },
    // 确定
    confirm() {
      this.visible = false;
      this.$nextTick(() => {
        this.$emit("confirm", this.currentVipCard);
      });
    },
    // 取消事件
    cancel() {
      this.visible = false;
      this.$nextTick(() => {
        this.$emit("cancel");
      });
    }
  }
};
</script>
